﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Threading;
using LineOfBusiness.Core.Base;
using LineOfBusiness.Controls.Base;
using LineOfBusiness.Poco;
using LineOfBusiness.DataAccess;
using LineOfBusiness.Core.Helper;

namespace LineOfBusiness.AD.Controllers
{
    [PageAttribute(EntityChild = "SR_tblSalesOrdersLines", EntityMater = "SR_tblSalesOrders")]
    public class SalesOrdersController : LOBController
    {
        public SalesOrdersController()
        {
            this.GridViewName = "grvtblSalesOrders";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ViewBag.Edit = 0;
            SR_tblSalesOrders GroupAccount = new SR_tblSalesOrders();

            if (ViewData.ContainsKey(ViewDataKeys.Predicate))
                ViewData.Remove(ViewDataKeys.Predicate);

            if (ViewData.ContainsKey(ViewDataKeys.DataValue))
                ViewData.Remove(ViewDataKeys.DataValue);

            return View("Input", GroupAccount);
        }

        public ActionResult View(string SONo)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                SR_tblSalesOrders SalesOrders = dal.Repository.GetQuery<SR_tblSalesOrders>().Where(o => o.SONo == SONo).FirstOrDefault();
                ViewBag.Edit = -1;
                return View("Input", SalesOrders);
            }
        }

        public ActionResult Edit(string SONo)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                SR_tblSalesOrders SalesOrders = dal.Repository.GetQuery<SR_tblSalesOrders>().Where(o => o.SONo == SONo).FirstOrDefault();
                if (this.Request.QueryString.AllKeys.Contains("View"))
                    ViewBag.Edit = -1;
                else
                    ViewBag.Edit = 1;

                return View("Input", SalesOrders);
            }
        }

        [HttpPost]
        public ActionResult Delete(SR_tblSalesOrders GroupAccount)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    dal.Repository.Delete(GroupAccount);
                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        [HttpPost]
        public ActionResult Save(object SalesOrders)
        {
            string method = "Edit";
            string KeyOld = "";

            if (ViewData["Edit"].Equals("0"))
            {
                method = "Add";
            }
            else
            {
                KeyOld = ViewData["ID"] as string;
            }

            if (SalesOrders != null)
            {
                foreach (var item in (SalesOrders as SR_tblSalesOrders).SR_tblSalesOrdersLines)
                {
                    item.RecordID = Guid.NewGuid();
                    item.SONo = (SalesOrders as SR_tblSalesOrders).SONo;
                    item.ItemType = "1";

                }
            }
            var salesOrders = (SalesOrders as SR_tblSalesOrders);
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    if (method == "Add")
                    {                        
                        dal.Repository.Add(salesOrders);
                    }
                    else
                    {
                            dal.Repository.Delete<SR_tblSalesOrders>(o => o.SONo == KeyOld);
                            dal.Repository.Delete<SR_tblSalesOrdersLines>(o => o.SONo == KeyOld);
                            dal.Repository.Add(salesOrders);
                    }

                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        public ActionResult ViewModel(string SONo)
        {
            this.ViewData[ViewDataKeys.EntityName] = "SR_ItemModels";
            this.ViewData[ViewDataKeys.GridViewName] = "grvItemModels";
            this.ViewData[ViewDataKeys.FormName] = "ItemModels";
            ViewBag.Title = "Hàng mẫu";
            ViewBag.SoNo = SONo;

            #region List translate label

            Dictionary<string, SYS_FormLabels> listLabel = new Dictionary<string, SYS_FormLabels>();

            listLabel = CacheHelper.GetFormLabel("ItemModels");

            ViewData.Add(ViewDataKeys.TranslateLabels, listLabel);

            #endregion


            #region GridViewSetup

            var gridViewSetups = CacheHelper.GetGridViewSetups("ItemModels", "grvItemModels");
                ViewData.Add(ViewDataKeys.GridViewSetups, gridViewSetups);
            
            #endregion

            return View();
        }

    }
}
